package com.nanhu.test.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.nanhu.test.R;
import com.nanhu.test.adapter.MyBaseAdapterActivity.MyAdapter.ViewHolder;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MyBaseAdapterActivity extends Activity{
	
	private ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.base_adapter_layout);
		listView = (ListView)findViewById(R.id.myListView);
		MyAdapter adapter = new MyAdapter(this);
		listView.setAdapter(adapter);
		listView.setItemsCanFocus(false);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ViewHolder vHollder = (ViewHolder) view.getTag();    
                vHollder.cBox.toggle();    
                MyAdapter.isSelected.put(position, vHollder.cBox.isChecked());
			}    
             
        });
	}
	
	public static class MyAdapter extends BaseAdapter{
		
		private LayoutInflater mInflater;
		
		private List<Map<String, Object>> mData;
		
		public static Map<Integer, Boolean> isSelected;
		
		public MyAdapter(Context context){
			mInflater = LayoutInflater.from(context);
			init();
		}
		
		private void init(){
			mData = new ArrayList<Map<String, Object>>();
			for(int i = 0; i < 5; i++){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("img", R.drawable.ic_launcher);
				map.put("title", "第" + (i + 1) + "行的标题");
				mData.add(map);
			}
			
			isSelected = new HashMap<Integer, Boolean>();
			for(int i = 0; i < mData.size(); i++){
				isSelected.put(i, false);
			}
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(convertView == null){
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.vlist, null);
				holder.img = (ImageView)convertView.findViewById(R.id.img);
				holder.title = (TextView)convertView.findViewById(R.id.title);
				holder.cBox = (CheckBox)convertView.findViewById(R.id.cb);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder)convertView.getTag();
			}
			holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
			holder.title.setText(mData.get(position).get("title").toString());
			holder.cBox.setChecked(isSelected.get(position));
			return convertView;
		}
		
		public final class ViewHolder {    
	        public ImageView img;    
	        public TextView title;    
	        public CheckBox cBox;    
	    }
	}
}
